{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 预测共享单车骑行量--模型训练"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 1. 对全体数据，随机选择其中80%做训练数据，剩下20%为测试数据，评价指标为RMSE。（10分）\n",
    "- 2. 用训练数据训练最小二乘线性回归模型（20分）、岭回归模型、Lasso模型，其中岭回归模型（30分）和Lasso模型（30分），注意岭回归模型和Lasso模型的正则超参数调优。\n",
    "- 3. 比较用上述三种模型得到的各特征的系数，以及各模型在测试集上的性能。并简单说明原因。（10分）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np \n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "from sklearn.linear_model import LinearRegression,RidgeCV,LassoCV\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import mean_squared_error,r2_score\n",
    "from sklearn.preprocessing import MinMaxScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>mnth_2</th>\n",
       "      <th>mnth_3</th>\n",
       "      <th>mnth_4</th>\n",
       "      <th>mnth_5</th>\n",
       "      <th>...</th>\n",
       "      <th>weathersit_2</th>\n",
       "      <th>weathersit_3</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>holiday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>yr</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.355170</td>\n",
       "      <td>0.373517</td>\n",
       "      <td>0.828620</td>\n",
       "      <td>0.284606</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.379232</td>\n",
       "      <td>0.360541</td>\n",
       "      <td>0.715771</td>\n",
       "      <td>0.466215</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>801</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   instant  season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  \\\n",
       "0        1         1         0         0         0       1       0       0   \n",
       "1        2         1         0         0         0       1       0       0   \n",
       "\n",
       "   mnth_4  mnth_5  ...  weathersit_2  weathersit_3      temp     atemp  \\\n",
       "0       0       0  ...             1             0  0.355170  0.373517   \n",
       "1       0       0  ...             1             0  0.379232  0.360541   \n",
       "\n",
       "        hum  windspeed  holiday  workingday  yr  cnt  \n",
       "0  0.828620   0.284606        0           0   0  985  \n",
       "1  0.715771   0.466215        0           0   0  801  \n",
       "\n",
       "[2 rows x 35 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('bike_day.csv')\n",
    "data.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>mnth_2</th>\n",
       "      <th>mnth_3</th>\n",
       "      <th>mnth_4</th>\n",
       "      <th>mnth_5</th>\n",
       "      <th>...</th>\n",
       "      <th>weathersit_1</th>\n",
       "      <th>weathersit_2</th>\n",
       "      <th>weathersit_3</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>holiday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>yr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.355170</td>\n",
       "      <td>0.373517</td>\n",
       "      <td>0.828620</td>\n",
       "      <td>0.284606</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.379232</td>\n",
       "      <td>0.360541</td>\n",
       "      <td>0.715771</td>\n",
       "      <td>0.466215</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 34 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   instant  season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  \\\n",
       "0        1         1         0         0         0       1       0       0   \n",
       "1        2         1         0         0         0       1       0       0   \n",
       "\n",
       "   mnth_4  mnth_5  ...  weathersit_1  weathersit_2  weathersit_3      temp  \\\n",
       "0       0       0  ...             0             1             0  0.355170   \n",
       "1       0       0  ...             0             1             0  0.379232   \n",
       "\n",
       "      atemp       hum  windspeed  holiday  workingday  yr  \n",
       "0  0.373517  0.828620   0.284606        0           0   0  \n",
       "1  0.360541  0.715771   0.466215        0           0   0  \n",
       "\n",
       "[2 rows x 34 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_y = pd.DataFrame(data,columns=['cnt'])\n",
    "train_x = data.drop(['cnt'],axis=1)\n",
    "train_x.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 最小二乘线性回归训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(584, 34)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "featrue_names = train_x.columns\n",
    "X_train,X_test,y_train,y_test = train_test_split(train_x,train_y,random_state=1,test_size=0.2)\n",
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 模型实例化并训练模型和预测模型\n",
    "lr = LinearRegression()\n",
    "lr.fit(X_train,y_train)\n",
    "y_train_predict = lr.predict(X_train)\n",
    "y_test_predict = lr.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>colums</th>\n",
       "      <th>corr_w</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>mnth_1</td>\n",
       "      <td>-2050.082295</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>29</td>\n",
       "      <td>hum</td>\n",
       "      <td>-1663.368927</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>mnth_2</td>\n",
       "      <td>-1642.161073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>30</td>\n",
       "      <td>windspeed</td>\n",
       "      <td>-1372.813376</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>26</td>\n",
       "      <td>weathersit_3</td>\n",
       "      <td>-1192.450755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>mnth_3</td>\n",
       "      <td>-821.198431</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>season_1</td>\n",
       "      <td>-698.046875</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>mnth_4</td>\n",
       "      <td>-565.167476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>31</td>\n",
       "      <td>holiday</td>\n",
       "      <td>-340.399835</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>17</td>\n",
       "      <td>weekday_0</td>\n",
       "      <td>-204.481384</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>18</td>\n",
       "      <td>weekday_1</td>\n",
       "      <td>-157.381097</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>19</td>\n",
       "      <td>weekday_2</td>\n",
       "      <td>-76.584124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>11</td>\n",
       "      <td>mnth_7</td>\n",
       "      <td>-63.703766</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>mnth_5</td>\n",
       "      <td>-57.503344</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>season_3</td>\n",
       "      <td>-46.103716</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>instant</td>\n",
       "      <td>-8.885890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>22</td>\n",
       "      <td>weekday_5</td>\n",
       "      <td>-5.994934</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>20</td>\n",
       "      <td>weekday_3</td>\n",
       "      <td>38.938938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>21</td>\n",
       "      <td>weekday_4</td>\n",
       "      <td>48.972861</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>10</td>\n",
       "      <td>mnth_6</td>\n",
       "      <td>75.431720</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>season_2</td>\n",
       "      <td>77.740682</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>32</td>\n",
       "      <td>workingday</td>\n",
       "      <td>188.351479</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>23</td>\n",
       "      <td>weekday_6</td>\n",
       "      <td>356.529740</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>25</td>\n",
       "      <td>weathersit_2</td>\n",
       "      <td>392.062306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>12</td>\n",
       "      <td>mnth_8</td>\n",
       "      <td>597.409231</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>season_4</td>\n",
       "      <td>666.409908</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>24</td>\n",
       "      <td>weathersit_1</td>\n",
       "      <td>800.388449</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>15</td>\n",
       "      <td>mnth_11</td>\n",
       "      <td>903.658147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>16</td>\n",
       "      <td>mnth_12</td>\n",
       "      <td>990.627747</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>28</td>\n",
       "      <td>atemp</td>\n",
       "      <td>1156.237739</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14</td>\n",
       "      <td>mnth_10</td>\n",
       "      <td>1181.688500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>13</td>\n",
       "      <td>mnth_9</td>\n",
       "      <td>1451.001041</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>27</td>\n",
       "      <td>temp</td>\n",
       "      <td>2326.269532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>33</td>\n",
       "      <td>yr</td>\n",
       "      <td>5303.985234</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          colums       corr_w\n",
       "5         mnth_1 -2050.082295\n",
       "29           hum -1663.368927\n",
       "6         mnth_2 -1642.161073\n",
       "30     windspeed -1372.813376\n",
       "26  weathersit_3 -1192.450755\n",
       "7         mnth_3  -821.198431\n",
       "1       season_1  -698.046875\n",
       "8         mnth_4  -565.167476\n",
       "31       holiday  -340.399835\n",
       "17     weekday_0  -204.481384\n",
       "18     weekday_1  -157.381097\n",
       "19     weekday_2   -76.584124\n",
       "11        mnth_7   -63.703766\n",
       "9         mnth_5   -57.503344\n",
       "3       season_3   -46.103716\n",
       "0        instant    -8.885890\n",
       "22     weekday_5    -5.994934\n",
       "20     weekday_3    38.938938\n",
       "21     weekday_4    48.972861\n",
       "10        mnth_6    75.431720\n",
       "2       season_2    77.740682\n",
       "32    workingday   188.351479\n",
       "23     weekday_6   356.529740\n",
       "25  weathersit_2   392.062306\n",
       "12        mnth_8   597.409231\n",
       "4       season_4   666.409908\n",
       "24  weathersit_1   800.388449\n",
       "15       mnth_11   903.658147\n",
       "16       mnth_12   990.627747\n",
       "28         atemp  1156.237739\n",
       "14       mnth_10  1181.688500\n",
       "13        mnth_9  1451.001041\n",
       "27          temp  2326.269532\n",
       "33            yr  5303.985234"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显示特征权重系数\n",
    "featrue_corr = pd.DataFrame({'colums':list(featrue_names),'corr_w':list((lr.coef_[0,:].T))})\n",
    "# featrue_corr\n",
    "featrue_corr.sort_values(by='corr_w')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "线性回归下训练集中对骑行量的RMSE评估:757.5047131675917\n",
      "线性回归下测试集中对骑行量的RMSE评估:754.8144924614058\n"
     ]
    }
   ],
   "source": [
    "# 用RMSE对模型评估\n",
    "# 训练集\n",
    "RMSE_train = np.sqrt(mean_squared_error(y_train,y_train_predict))\n",
    "print(\"线性回归下训练集中对骑行量的RMSE评估:{}\".format(RMSE_train))\n",
    "# 测试集\n",
    "RMSE_test = np.sqrt(mean_squared_error(y_test,y_test_predict))\n",
    "print(\"线性回归下测试集中对骑行量的RMSE评估:{}\".format(RMSE_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "线性回归下训练集中对骑行量的R2_score评估:0.9196376061169373\n",
      "线性回归下测试集中对骑行量的R2_score评估:0.9196900809977304\n"
     ]
    }
   ],
   "source": [
    "# 用r2_score对最小二乘线性回归模型评估\n",
    "# 训练集\n",
    "R2_train = np.sqrt(r2_score(y_train,y_train_predict))\n",
    "print(\"线性回归下训练集中对骑行量的R2_score评估:{}\".format(R2_train))\n",
    "#  测试集\n",
    "R2_test = np.sqrt(r2_score(y_test,y_test_predict))\n",
    "print(\"线性回归下测试集中对骑行量的R2_score评估:{}\".format(R2_test))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 用RidgeCV训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "alphas = [0.001,0.01,0.1,10,100,1000] \n",
    "ridge_CV = RidgeCV(alphas=alphas,store_cv_values=True)\n",
    "ridge_CV.fit(X_train,y_train)\n",
    "y_train_predict1 = ridge_CV.predict(X_train)\n",
    "y_test_predict1 = ridge_CV.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>colums</th>\n",
       "      <th>corr_w</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>29</td>\n",
       "      <td>hum</td>\n",
       "      <td>-1664.628231</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>30</td>\n",
       "      <td>windspeed</td>\n",
       "      <td>-1366.652682</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>mnth_1</td>\n",
       "      <td>-1306.300778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>26</td>\n",
       "      <td>weathersit_3</td>\n",
       "      <td>-1190.235793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>mnth_2</td>\n",
       "      <td>-1027.535308</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>season_1</td>\n",
       "      <td>-709.559208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>mnth_3</td>\n",
       "      <td>-346.944576</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>31</td>\n",
       "      <td>holiday</td>\n",
       "      <td>-335.570245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>mnth_4</td>\n",
       "      <td>-220.016288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>17</td>\n",
       "      <td>weekday_0</td>\n",
       "      <td>-203.993867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>18</td>\n",
       "      <td>weekday_1</td>\n",
       "      <td>-158.413685</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>11</td>\n",
       "      <td>mnth_7</td>\n",
       "      <td>-126.360175</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>19</td>\n",
       "      <td>weekday_2</td>\n",
       "      <td>-78.430117</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>season_3</td>\n",
       "      <td>-35.949078</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>instant</td>\n",
       "      <td>-4.428012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>22</td>\n",
       "      <td>weekday_5</td>\n",
       "      <td>-3.441233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>20</td>\n",
       "      <td>weekday_3</td>\n",
       "      <td>40.991515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>21</td>\n",
       "      <td>weekday_4</td>\n",
       "      <td>49.008289</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>season_2</td>\n",
       "      <td>72.604507</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>mnth_5</td>\n",
       "      <td>158.763513</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>10</td>\n",
       "      <td>mnth_6</td>\n",
       "      <td>158.879759</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>32</td>\n",
       "      <td>workingday</td>\n",
       "      <td>185.285015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>16</td>\n",
       "      <td>mnth_12</td>\n",
       "      <td>236.176305</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>15</td>\n",
       "      <td>mnth_11</td>\n",
       "      <td>276.657199</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>23</td>\n",
       "      <td>weekday_6</td>\n",
       "      <td>354.279098</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>25</td>\n",
       "      <td>weathersit_2</td>\n",
       "      <td>391.629799</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>12</td>\n",
       "      <td>mnth_8</td>\n",
       "      <td>394.873237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>season_4</td>\n",
       "      <td>672.903779</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14</td>\n",
       "      <td>mnth_10</td>\n",
       "      <td>695.729717</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>24</td>\n",
       "      <td>weathersit_1</td>\n",
       "      <td>798.605994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>13</td>\n",
       "      <td>mnth_9</td>\n",
       "      <td>1106.077397</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>28</td>\n",
       "      <td>atemp</td>\n",
       "      <td>1337.860579</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>27</td>\n",
       "      <td>temp</td>\n",
       "      <td>2115.075754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>33</td>\n",
       "      <td>yr</td>\n",
       "      <td>3672.325862</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          colums       corr_w\n",
       "29           hum -1664.628231\n",
       "30     windspeed -1366.652682\n",
       "5         mnth_1 -1306.300778\n",
       "26  weathersit_3 -1190.235793\n",
       "6         mnth_2 -1027.535308\n",
       "1       season_1  -709.559208\n",
       "7         mnth_3  -346.944576\n",
       "31       holiday  -335.570245\n",
       "8         mnth_4  -220.016288\n",
       "17     weekday_0  -203.993867\n",
       "18     weekday_1  -158.413685\n",
       "11        mnth_7  -126.360175\n",
       "19     weekday_2   -78.430117\n",
       "3       season_3   -35.949078\n",
       "0        instant    -4.428012\n",
       "22     weekday_5    -3.441233\n",
       "20     weekday_3    40.991515\n",
       "21     weekday_4    49.008289\n",
       "2       season_2    72.604507\n",
       "9         mnth_5   158.763513\n",
       "10        mnth_6   158.879759\n",
       "32    workingday   185.285015\n",
       "16       mnth_12   236.176305\n",
       "15       mnth_11   276.657199\n",
       "23     weekday_6   354.279098\n",
       "25  weathersit_2   391.629799\n",
       "12        mnth_8   394.873237\n",
       "4       season_4   672.903779\n",
       "14       mnth_10   695.729717\n",
       "24  weathersit_1   798.605994\n",
       "13        mnth_9  1106.077397\n",
       "28         atemp  1337.860579\n",
       "27          temp  2115.075754\n",
       "33            yr  3672.325862"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显示特征权重系数\n",
    "featrue_corr1 = pd.DataFrame({'colums':list(featrue_names),'corr_w':list((ridge_CV.coef_[0,:].T))})\n",
    "# featrue_corr1\n",
    "featrue_corr1.sort_values(by='corr_w')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "岭回归模型下训练集中对骑行量的R2_score评估:0.9194089007333841\n",
      "岭回归模型下测试集中对骑行量的R2_score评估:0.9210551437308832\n"
     ]
    }
   ],
   "source": [
    "# 用r2_score对岭回归模型评估\n",
    "# 训练集\n",
    "R2_train1 = np.sqrt(r2_score(y_train,y_train_predict1))\n",
    "print(\"岭回归模型下训练集中对骑行量的R2_score评估:{}\".format(R2_train1))\n",
    "#  测试集\n",
    "R2_test1 = np.sqrt(r2_score(y_test,y_test_predict1))\n",
    "print(\"岭回归模型下测试集中对骑行量的R2_score评估:{}\".format(R2_test1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 用Lasso回归训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\program files\\python\\python37\\lib\\site-packages\\sklearn\\linear_model\\coordinate_descent.py:1100: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n",
      "d:\\program files\\python\\python37\\lib\\site-packages\\sklearn\\model_selection\\_split.py:1978: FutureWarning: The default value of cv will change from 3 to 5 in version 0.22. Specify it explicitly to silence this warning.\n",
      "  warnings.warn(CV_WARNING, FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "lass_CV = LassoCV()\n",
    "lass_CV.fit(X_train,y_train)\n",
    "y_train_predict2 = lass_CV.predict(X_train)\n",
    "y_test_predict2 = lass_CV.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "岭回归模型下训练集中对骑行量的R2_score评估:0.6695067435087584\n",
      "岭回归模型下测试集中对骑行量的R2_score评估:0.6286397931564823\n"
     ]
    }
   ],
   "source": [
    "# 用r2_score对岭回归模型评估\n",
    "# 训练集\n",
    "R2_train2 = np.sqrt(r2_score(y_train,y_train_predict2))\n",
    "print(\"岭回归模型下训练集中对骑行量的R2_score评估:{}\".format(R2_train2))\n",
    "#  测试集\n",
    "R2_test2 = np.sqrt(r2_score(y_test,y_test_predict2))\n",
    "print(\"岭回归模型下测试集中对骑行量的R2_score评估:{}\".format(R2_test2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>colums</th>\n",
       "      <th>corr_w</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>season_1</td>\n",
       "      <td>-303.134059</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>16</td>\n",
       "      <td>mnth_12</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>19</td>\n",
       "      <td>weekday_2</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>20</td>\n",
       "      <td>weekday_3</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>21</td>\n",
       "      <td>weekday_4</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>22</td>\n",
       "      <td>weekday_5</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>23</td>\n",
       "      <td>weekday_6</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>24</td>\n",
       "      <td>weathersit_1</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>25</td>\n",
       "      <td>weathersit_2</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>26</td>\n",
       "      <td>weathersit_3</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>27</td>\n",
       "      <td>temp</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>28</td>\n",
       "      <td>atemp</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>29</td>\n",
       "      <td>hum</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>30</td>\n",
       "      <td>windspeed</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>31</td>\n",
       "      <td>holiday</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>18</td>\n",
       "      <td>weekday_1</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>17</td>\n",
       "      <td>weekday_0</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>33</td>\n",
       "      <td>yr</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>15</td>\n",
       "      <td>mnth_11</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14</td>\n",
       "      <td>mnth_10</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>13</td>\n",
       "      <td>mnth_9</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>12</td>\n",
       "      <td>mnth_8</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>11</td>\n",
       "      <td>mnth_7</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>10</td>\n",
       "      <td>mnth_6</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>mnth_5</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>mnth_4</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>mnth_3</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>mnth_2</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>mnth_1</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>season_4</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>season_3</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>season_2</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>32</td>\n",
       "      <td>workingday</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>instant</td>\n",
       "      <td>5.650325</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          colums      corr_w\n",
       "1       season_1 -303.134059\n",
       "16       mnth_12   -0.000000\n",
       "19     weekday_2   -0.000000\n",
       "20     weekday_3    0.000000\n",
       "21     weekday_4    0.000000\n",
       "22     weekday_5    0.000000\n",
       "23     weekday_6    0.000000\n",
       "24  weathersit_1    0.000000\n",
       "25  weathersit_2   -0.000000\n",
       "26  weathersit_3   -0.000000\n",
       "27          temp    0.000000\n",
       "28         atemp    0.000000\n",
       "29           hum   -0.000000\n",
       "30     windspeed   -0.000000\n",
       "31       holiday   -0.000000\n",
       "18     weekday_1   -0.000000\n",
       "17     weekday_0   -0.000000\n",
       "33            yr    0.000000\n",
       "15       mnth_11   -0.000000\n",
       "14       mnth_10   -0.000000\n",
       "13        mnth_9    0.000000\n",
       "12        mnth_8    0.000000\n",
       "11        mnth_7    0.000000\n",
       "10        mnth_6    0.000000\n",
       "9         mnth_5    0.000000\n",
       "8         mnth_4    0.000000\n",
       "7         mnth_3   -0.000000\n",
       "6         mnth_2   -0.000000\n",
       "5         mnth_1   -0.000000\n",
       "4       season_4   -0.000000\n",
       "3       season_3    0.000000\n",
       "2       season_2    0.000000\n",
       "32    workingday    0.000000\n",
       "0        instant    5.650325"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显示特征权重系数\n",
    "featrue_corr2 = pd.DataFrame({'colums':list(featrue_names),'corr_w':list((lass_CV.coef_.T))})\n",
    "# featrue_corr1\n",
    "featrue_corr2.sort_values(by='corr_w')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模型对比\n",
    "#### 通过在三种模型上对比得出的结果可以看出：\n",
    "- 最小二乘法模型得出的结果在训练集和测试集上表现的性能较好\n",
    "- 相对最小二乘法，岭回归模型训练的结果在测试集上较训练集上好\n",
    "- Lasso回归由于正则项为L1正则，出项了许多特征为0的现象，相比于前两种模型，在训练集和测试集上结果较差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
